package com.apes.auto.autoparts.model;


import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Arrays;


@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = EpcPartNumberParseList.CONST_TABLE_NAME)
public class EpcPartNumberParseList extends BaseModel {

    // 历史备份表
    // epc_partnumber_parse_bak_05_22
    // epc_partnumber_parse_list_bak1


    public static final String CONST_TABLE_NAME = "epc_partnumber_parse_list";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "来源编码")
    private String id;


    @ManyToOne(optional = false)
    @JoinColumn(name = "epc_partnumber_parse_id", referencedColumnName = "id", nullable = false)
    @Comment("epc解析单号")
    private EpcPartNumberParse epcPartNumberParse;


    @Comment("图号")
    @Column(name = "svn", length = 120)
    private String svn;

    @Comment("OEM码")
    @Column(name = "PARTNUMBER", length = 120)
    private String partNumber;


    @Comment("OEM描述")
    @Column(name = "description", length = 4000)
    private String description;


    @Comment("配件名称")
    @Column(name = "part_description", length = 4000)
    private String partDescription;

    @Comment("外部渠道名称")
    @Column(name = "part_description_ext", length = 4000)
    private String partDescriptionExt;

    @Comment("精友配件名称")
    @Column(name = "part_description_jingyou", length = 4000)
    private String partDescriptionJingyou;

    @Comment("时间戳")
    @Column(name = "ts01", length = 120)
    private String ts01;

    @Comment("排序图片")
    @Column(name = "img_order", length = 4000)
    private String imgOrder;

    @Comment("图片")
    @Column(name = "img_name", length = 4000)
    private String imgName;

    @Comment("修改标记")
    @Column(name = "change_sign", nullable = false)
    private boolean changeSign = false;


    @Transient
    private String[] images;

    public String[] getImages() {
        if( this.imgName != null ) {
            String replace = this.imgName.replace(".tif", ".TIF");
//            http://10.2.15/BMW/003/471/ib2cf.jpg
//            003/471/ib2cf.jpg,003/471/ib2cf.jpg;003/471/ib2cf.jpg
            return Arrays.stream(replace.replaceAll(",",";").split(";"))
                    .map(item -> "remoteEpcImage/" + this.epcPartNumberParse.getEpcVersion() + "/" + item)
                    .toArray(String[]::new);
        } else {
            return new String[0];
        }
    }

    public String getImgOrder() {
        if (this.imgOrder != null) return this.imgOrder.replace(".tif", ".TIF");
        return this.imgOrder;
    }
}
